﻿var App = Ember.Application.create({
    // 定义了应用出口
    rootElement: '#app',

    LOG_TRANSITIONS: true, // basic logging of successful transitions
    LOG_TRANSITIONS_INTERNAL: true // detailed logging of all routing steps
});

App.ApplicationAdapter = DS.FixtureServerAdapter.extend({});

Ember.Application.initializer({
    name: "storeInject",

    initialize: function (container, application) {
        application.store = container.lookup("store:main");
    }
});

//hub
(function () {
    var user = $.connection.userHub;

    $.extend(user.client, {

        Create: function (model) {
            var json = App.UserModel.FIXTURES.filter(function (d) {
                return d.id == model.id;
            });

            if (!json[0]) {
                App.store.push('user', model);
                App.UserModel.FIXTURES.push(model);
            }
        },

        Update: function (model) {
            var userModel = App.store.getById('user', model.id);
            userModel.setProperties(model);
        },

        Delete: function (model) {
            var json = App.UserModel.FIXTURES.filter(function (d) {
                return d.id == model.id;
            });

            if (json[0]) {
                var userModel = App.store.getById('user', model.id);
                App.store.deleteRecord(userModel);
                var index = App.UserModel.FIXTURES.indexOf(json[0]);
                App.UserModel.FIXTURES.slice(index, 1);
            }
        }
    });

    $.connection.hub.start();

    App.hub = user;

})();